<!DOCTYPE html>
<!--suppress ALL -->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>attempt(尝试)</title>
</head>
<body>
<h4>
    Attempts to invoke a function with the provided arguments, returning either the result or the caught error object.
    Use a try... catch block to return either the result of the function or an appropriate error.
</h4>
<h4>
    采用try... catch来捕捉异常并返回
</h4>
<script type="text/javascript">
    const attempt = (fn, ...args) => {
        try {
            return fn(...args);
        } catch (e) {
            return e instanceof Error ? e : new Error(e);
        }
    };
    var elements = attempt(function (selector) {
        //传入css选择器对象进行查找
        //如果指定的选择器不合法,就会抛出错误
        return document.querySelectorAll(selector);
    }, '>_>');
    if (elements instanceof Error) {
        console.log(elements.message)
    }
</script>
</body>
</html>